package com.cskaoyan.exception;

import com.cskaoyan.bean.ExceptionInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author stone
 * @date 2022/05/03 17:10
 */
//@ResponseBody
//@ControllerAdvice
@RestControllerAdvice
public class CustomControllerAdvice {

    //定义ExceptionHandler方法
    //@ExceptionHandler(value = {ArithmeticException.class})
    /*@ExceptionHandler(ArithmeticException.class)
    public String handleArithmeticException() {
        return "/exception.jsp";//返回值字符串作为视图名
    }*/


    /*@ExceptionHandler(ArithmeticException.class)
    public ModelAndView handleArithmeticException() {
        return new ModelAndView("/exception.jsp");//返回值ModelAndView
    }*/

    //@ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public ExceptionInfo handleArithmeticException(ArithmeticException exception) {//形参中可以直接获得抛出的异常
        ExceptionInfo exceptionInfo = new ExceptionInfo();
        exceptionInfo.setNo(500);
        exceptionInfo.setMessage(exception.getMessage());
        return exceptionInfo;
    }

}
